package com.cn.integration.business.invoicecenter.controller;

import com.cn.common.aliyun.OssUtil;
import com.cn.common.model.AuthUser;
import com.cn.common.model.Result;
import com.cn.common.utils.excel.ExcelUtil;
import com.cn.integration.business.invoicecenter.dto.InvoiceConfDto;
import com.cn.integration.business.invoicecenter.service.InvoiceConfService;
import com.cn.integration.business.invoicecenter.vo.InvoiceConfVo;
import com.cn.integration.common.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.io.FileInputStream;
import java.util.List;

@RestController
@RequestMapping("/web/ump/invoiceConf")
public class InvoiceConfController extends BaseController {

    @Resource
    private InvoiceConfService invoiceConfService;
    @Resource
    private OssUtil ossUtil;

    @PostMapping("/queryInvoiceConfPage")
    public Result<PageInfo<InvoiceConfVo>> queryInvoiceConfPage(@RequestBody InvoiceConfDto dto) {
        // 企业子账号数据权限控制
        AuthUser authUser = this.getAuthUser();
        Long userId = null;
        if(authUser != null){
            userId = authUser.getUserId();
        }
        dto.setUserId(userId);
        return Result.success(invoiceConfService.queryInvoiceConfPage(dto));
    }

    @PostMapping("/export")
    public Result<String> export(@RequestBody InvoiceConfDto dto) {
        // 企业子账号数据权限控制
        AuthUser authUser = this.getAuthUser();
        Long userId = null;
        if(authUser != null){
            userId = authUser.getUserId();
        }
        dto.setUserId(userId);
        String fileName = "开票配置";
        List<InvoiceConfVo> list = invoiceConfService.queryInvoiceConfList(dto);
        FileInputStream fileInputStream = ExcelUtil.exportExcelToOSS(InvoiceConfVo.class, fileName, list);
        String url = ossUtil.upload("export/excel/" + userId + "/票据配置列表.xlsx", fileInputStream);
        //TODO 导出记录入库
        return Result.success(url);
    }

}
